
import sys, os, re
import urllib
from lxml import etree
from dateutil import parser as dateparser
from baseHandler import *

#baseuri = "http://web.archive.org/web/*xm_sa_/"
baseuri = "http://wayback.archive.org/web/xmlquery?url=%s&startdate=1996"

# This access point doesn't have entire holdings
#baseuri = "http://wwwoh-access.archive.org:8080/wwwoh/xmlquery?type=urlquery&url="

class IArchiveHandler(BaseProxyHandler):

    def fetch_changes(self, req, requri, dt=None):
        # implement the changes list for this particular proxy

        uri = baseuri % requri
        try:
            fh = urllib.urlopen(uri)
        except:
            return self.error("Couldn't retrieve data from %s" % uri, req, status=404)
        data = fh.read()
        fh.close()

        try:
            dom = etree.XML(data)
        except:
            return self.error("Couldn't parse data from %s" % uri, req, status=500)

        if not dom:
            return

        changes = []

        rlist = dom.xpath('//result')
        for a in rlist:

            dtstr = (a.xpath("./capturedate"))[0].text
            loc = "http://web.archive.org/web/%s/%s" % (dtstr, (a.xpath("./url"))[0].text)
            dtstr += " GMT"
            dtobj = dateparser.parse(dtstr)
            if changes and changes[-1][0] == changes[-1][2]['last']:
                changes[-1][2]['last'] = dtobj
            if a.tail:
                changes.append((dtobj, loc, {'last' : dtobj, 'obs' : 1, 'type' : 'observed'}))
            else:
                changes[-1][-1]['last'] = dtobj
                changes[-1][-1]['obs'] += 1

        changes.sort()
        return changes


def handler(req):
    hdlr = IArchiveHandler('ia')
    return basehandler(req, hdlr)


